<template>
  <div>
    <span style="color: red">PersonList 共有{{userList.length}}个人</span><br>
    <span>{{userName}}在学校学习{{subject}}</span><br>
    <span>当前求和：{{sum}}</span><br>
    <span>利用getters得到十倍后的值：{{bigNum}}</span><br>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd(n)">当前求和为奇数再加</button>
    <button @click="incrementWait(n)">等一等再加</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
export default {
  name: 'UseVuex2',
  data() {
    return {
      n: 1,
      num: 0,
    }
  },
  methods: {
    // increment() {
    //   this.$store.commit('saveAdd', this.n)
    // },
    // decrement() {
    //   this.$store.commit('savePlus', this.n)
    // },
    ...mapMutations({
      increment: 'saveAdd',
      decrement: 'savePlus'
    }),
    
    // incrementOdd() {
    //   this.$store.dispatch('addOdd', this.n)
    // },
    // incrementWait() {
    //   this.$store.dispatch('addWait', this.n)
    // }
    ...mapActions({
      incrementOdd: 'addOdd',
      incrementWait: 'addWait'
    })
  },
  computed: {
    // userName() {
    //   return this.$store.state.userName
    // },
    // subject() {
    //   return this.$store.state.subject
    // },
    // sum() {
    //   return this.$store.state.num
    // },
    ...mapState({
      userName: 'userName',
      subject: 'subject',
      sum: 'num',
      userList: 'userList'
    }),

    // bigNum() {
    //   return this.$store.getters.bigNum
    // },
    ...mapGetters(['bigNum']),
  },
  mounted() {
    // console.log(this)
  }
}
</script>

<style>

</style>